//
//  PLPicturePreviewManager.m
//  PlatformCommon
//
//  Created by 九叶子 on 2018/7/17.
//

#import "PLPicturePreviewManager.h"
#import "PLPhotoBrowser.h"

@interface PLPicturePreviewManager()<PLPhotoBrowserDelegate>

@property (nonatomic, strong)PLPhotoBrowser * photoBrower;
@end

@implementation PLPicturePreviewManager

static PLPicturePreviewManager *_sharedManager = nil;
+(instancetype)sharedInstance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedManager = [[PLPicturePreviewManager alloc]init];
    });
    return _sharedManager;
}

- (void)showImages{
    self.photoBrower = [[PLPhotoBrowser alloc]init];
    self.photoBrower.currentImageIndex = self.currentImageIndex;
    self.photoBrower.delegate = self;
    self.photoBrower.sourceImagesContainerView = self.sourceImagesContainerView;
    self.photoBrower.imageCount = self.highQualityImageArray.count;
    [self.photoBrower show];
}

- (UIImage *)photoBrowser:(PLPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index {
    if (self.placeholderImageArray.count > 0) {
        return self.placeholderImageArray[index];
    }else{
        return [UIImage imageNamed:@""];
    }
}

- (NSURL *)photoBrowser:(PLPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
{
    NSString *urlStr = self.highQualityImageArray[index];
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    return [NSURL URLWithString:urlStr];
}

- (void)photoBrowserDiddisappear:(PLPhotoBrowser *)browser{
    self.photoBrower = nil;
    self.currentImageIndex = 0;
    self.placeholderImageArray = nil;
    self.highQualityImageArray = nil;
    self.sourceImagesContainerView = nil;
}

@end
